using System;
using System.Xml; 
using System.Web;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class XmlReaderPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string fileName = Server.MapPath("XmlWriter.xml");
        XmlReader xmlReader = XmlReader.Create(fileName);
        
        StringBuilder str = new StringBuilder();
        while (xmlReader.Read())
        {
            switch (xmlReader.NodeType)
            {
                case XmlNodeType.XmlDeclaration:
                    str.Append("XML Declaration: <b>");
                    str.Append(xmlReader.Name);
                    str.Append(" ");
                    str.Append(xmlReader.Value);
                    str.Append("</b><br />");
                    break;
                case XmlNodeType.Element:
                    str.Append("    Element: <b>");
                    str.Append(xmlReader.Name);
                    str.Append(" ");
                    str.Append(xmlReader.GetAttribute("ID"));
                    str.Append("</b> <br />");
                    break;
                case XmlNodeType.Text:
                    str.Append(" - Value: <b>");
                    str.Append(xmlReader.Value);
                    str.Append("</b><br />");
                    break;
            }
        }
        xmlReader.Close();
        Literal1.Text = str.ToString();        
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string fileName = Server.MapPath("XmlWriter.xml");
        using (FileStream fs = new FileStream(fileName, FileMode.Open))
        {
            XmlReader xmlReader = XmlReader.Create(fs);
            StringBuilder str = new StringBuilder();
            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.XmlDeclaration:
                        str.Append("XML Declaration: <b>");
                        str.Append(xmlReader.Name);
                        str.Append(" ");
                        str.Append(xmlReader.Value);
                        str.Append("</b><br />");
                        break;
                    case XmlNodeType.Element:
                        str.Append("    Element: <b>");
                        str.Append(xmlReader.Name);
                        str.Append(" ");
                        str.Append(xmlReader.GetAttribute("ID"));
                        str.Append("</b> <br />");
                        break;
                    case XmlNodeType.Text:
                        str.Append(" - Value: <b>");
                        str.Append(xmlReader.Value);
                        str.Append("</b><br />");
                        break;
                }
            }
            xmlReader.Close();
            Literal1.Text = str.ToString();
        }
         
    }
}
